//
//  PortraitBtn.m
//  Cunli
//
//  Created by lilubin on 15/7/4.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "PortraitBtn.h"
#import "Config.h"
#import "UIButton+WebCache.h"
#import "User.h"

#define KCLPORTRAITCHANGE @"portraitChanged"

@implementation PortraitBtn

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self _addObserve];
        //        if ([Config getPortraitURL].length == 0) {
        //            [self loadPersonalData];
        //        }else
        //        {
        //            [self loadImage];
        //        }
        //        [self loadPersonalData];
        [self loadImage];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self _addObserve];
//    self.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [self loadImage];
    self.contentMode = UIViewContentModeScaleAspectFit;
}

- (void)_addObserve
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(portraitChange:) name:KCLPORTRAITCHANGE object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KCLPORTRAITCHANGE object:nil];
}

- (void)portraitChange:(NSNotification *)notification
{
    [self loadImage];
}

- (void)loadImage
{
    NSURL *porURL = [NSURL URLWithString:[Login curLoginDetailUser].pic];
//    NSURL *porURL =[NSURL URLWithString:[Config getPortraitURL]];
    NSLog(@"In portraitBtn porURL:%@", [Login curLoginDetailUser].pic);
    [self setImageWithURL:porURL placeholderImage:[UIImage imageNamed:@"default_news"]];
}

@end
